package moco.blatt9.soapwebservices;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Addition extends Activity{
	
	private TextView erg;
	private EditText nr1, nr2;
	private Button add;
	
	private final String HOST = "10.0.2.2:5145"; 
	private final String WSDL_URL = "http://" + HOST + "/WebServiceLocalServerTomcat/services/Additon?wsdl";
	private final String NAMESPACE = "http://soap.blatt9.moco";
	private final String SOAP_ACTION = NAMESPACE + "/add";
    private final String METHOD_NAME = "add";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addition);

		erg = (TextView) findViewById(R.id.ergebnis);
		nr1 = (EditText) findViewById(R.id.nr1);
		nr2 = (EditText) findViewById(R.id.nr2);
		add = (Button) findViewById(R.id.add);
		
		add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String z1 = nr1.getText().toString().replaceAll(",", "\\.");
				String z2 = nr2.getText().toString().replaceAll(",", "\\.");
				if( _pruefeEingabe(z1,z2) ) {
					
					new AsyncTask<String, String, Double>(){

						@Override
						protected Double doInBackground(String... zahlen) {
							
							return _soapAdd( Double.parseDouble(zahlen[0]), Double.parseDouble(zahlen[1]) );
						}
						
						protected void onPostExecute(Double result) {
							erg.setText(result.toString());
						};
						
					}.execute(z1, z2);
					
				}
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.call_local_activity) {
			Intent loc = new Intent();
			loc.setClassName(this, MainActivity.class.getName());
			startActivity(loc);
		} else if(item.getItemId() == R.id.call_tempr_activity){
			Intent cf = new Intent();
			cf.setClassName(this, CelsiusFahrenheit.class.getName());
			startActivity(cf);
		}
		return super.onOptionsItemSelected(item);
	}
	
	/* **************************************************************************
	 * 							PRIVATE METHODES								*
	 * ************************************************************************** */
	private boolean _pruefeEingabe(String nr1, String nr2){
		boolean ok = true;
		String falscheZahlen = "";
		try {
			Double.parseDouble(nr1);
		} catch (NumberFormatException ex) {
			falscheZahlen += "1";
			ok = false;
		}
		
		try {
			Double.parseDouble(nr2);
		} catch (NumberFormatException ex) {
			falscheZahlen += ok ? "2" : " und 2";
			ok = false;
		}
		
		if(!ok)
			Toast.makeText(this, "Die Eingabe (Zahl "+falscheZahlen+") ist keine Zahl!", Toast.LENGTH_LONG).show();
		
		return ok;
		
	}
	
	private Double _soapAdd(Double d1, Double d2){
		Double resd = Double.NEGATIVE_INFINITY;
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("nr1", d1.toString());
		request.addProperty("nr2", d2.toString());
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		
		try {
			
			HttpTransportSE httptransport = new HttpTransportSE(WSDL_URL);
			httptransport.call(SOAP_ACTION, envelope);
			
			if( envelope.bodyIn instanceof SoapFault ){
				
				SoapFault sf = (SoapFault) envelope.bodyIn;
				throw new Exception(sf.faultstring + ". " + sf.getMessage() + ". " + sf.getCause());
				
			} else {
				
				SoapObject response = (SoapObject) envelope.bodyIn;
				resd = Double.parseDouble(response.getProperty(0).toString());
				
			}
			
		} catch (IOException e) {
			System.out.println("Fehler1: " + e.getMessage() + ". " + e.getCause());
		} catch (XmlPullParserException e) {
			System.out.println("Fehler2: " + e.getMessage() + ". " + e.getCause());
		} catch (NumberFormatException e){
			System.out.println("Fehler3: " + e.getClass().getName() + ". " + e.getMessage() + ". " + e.getCause());
		} catch (Exception e) {
			System.out.println("Fehler4: " + e.getClass().getName() + ". " + e.getMessage() + ". " + e.getCause());
		}
		
		return resd;
	}

}
